package com.bio.dao.user.impl;

import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import com.bio.dao.impl.HibernateDaoImpl;
import com.bio.dao.impl.HibernateQuery;
import com.bio.dao.user.UserDAO;
import com.bio.entity.user.User;
import com.bio.utils.Pager;
import com.bio.utils.Pagination;

public class UserDAOImpl extends HibernateDaoImpl<User> implements UserDAO {

	private static final Integer FLAG_DELETED = 1;

	@Override
	public User login(String email, String password) {
		List<User> users = new HibernateQuery<User>(this.getSessionFactory(),
				this.getSession(), User.class).setPagination(null).addWhere(
				Restrictions.eq("email", email)).addWhere(
				Restrictions.eq("password", password)).query().getResult();

		return users.size() > 0 ? users.get(0) : null;
	}

	@Override
	public Pager<User> findPagerByCompanyId(Pagination pagination,
			Long companyId) {
		if (null == companyId) {
			return new HibernateQuery<User>(this.getSessionFactory(), this
					.getSession(), User.class).setPagination(pagination)
					.addWhere(Restrictions.isNull("companyId")).query();
		}
		return new HibernateQuery<User>(this.getSessionFactory(), this
				.getSession(), User.class).setPagination(pagination).addWhere(
				Restrictions.eq("companyId", companyId)).query();
	}

	@Override
	public void removeUsersByCompany(Long companyId) {
		String sql = "UPDATE user SET is_deleted = " + FLAG_DELETED
				+ " WHERE company_id =" + companyId;
		SQLQuery sq = this.getSession().createSQLQuery(sql);
		sq.executeUpdate();
	}

	@Override
	public void removeUsersByPosition(Long positionId) {
		String sql = "UPDATE user SET is_deleted = " + FLAG_DELETED
				+ " WHERE position_id =" + positionId;
		SQLQuery sq = this.getSession().createSQLQuery(sql);
		sq.executeUpdate();
	}

	@Override
	public User getUserByNameAndCompany(String name, Long companyId) {
		List<User> users = new HibernateQuery<User>(this.getSessionFactory(),
				this.getSession(), User.class).setPagination(null).addWhere(
				Restrictions.eq("userName", name)).addWhere(
				Restrictions.eq("companyId", companyId)).query().getResult();

		return users.size() > 0 ? users.get(0) : null;

	}

	@Override
	public int countUsersByName(final String userName, final Long id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		criteria.add(Restrictions.eq("userName", userName));
		if(null != id){
			criteria.add(Restrictions.not(Restrictions.eq("id", id)));
		}
		criteria.setProjection(Projections.rowCount());
		return countByCriteria(criteria);
	}

	@Override
	public int countUsersByMail(final String mail, final Long id) {
//		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
//		criteria.add(Restrictions.eq("email", mail));
//		if(null != id){
//			criteria.add(Restrictions.not(Restrictions.eq("id", id)));
//		}
//		//criteria.add(Restrictions.eq("isDeleted", 0));
//		criteria.setProjection(Projections.rowCount());
//		return countByCriteria(criteria);
		
		HibernateQuery hq = new HibernateQuery<User>(this.getSessionFactory(), this
				.getSession(), User.class).setPagination(null)
				.addWhere(Restrictions.eq("email",mail));
		if(null != id){
			hq.addWhere(Restrictions.not(Restrictions.eq("id", id)));
		}
		return hq.query().getResult().size();
	}

	@Override
	public User getUserByEmail(String email) {
		List<User> users = new HibernateQuery<User>(this.getSessionFactory(),
				this.getSession(), User.class).setPagination(null).addWhere(
				Restrictions.eq("email", email)).query().getResult();

		return users.size() > 0 ? users.get(0) : null;
	}
}
